﻿#ifndef PUBLICS_H
#define PUBLICS_H

// #include <sys/time.h>      //添加头文件
#include <stdio.h>
#include <time.h>
#include <chrono>
#include <iostream>
#include <vector>
#include <sstream>
#include <math.h>

using namespace std;

int64_t getCurrentTime()      //直接调用这个函数就行了，返回值最好是int64_t，long long应该也可以
{
    auto now = std::chrono::system_clock::now();
    uint64_t dis_millseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count()
            - std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count() * 1000;

  return dis_millseconds;
}

std::string  getTime(bool isDate)
{
   // 基于当前系统的当前日期/时间
   time_t now = time(0);
   tm *ltm = localtime(&now);


   if(isDate){
       int year = 1900 + ltm->tm_year;
       int mon  = 1 + ltm->tm_mon;
       int day = ltm->tm_mday;
       return std::to_string(year) + "_" + std::to_string(mon) + "_" +  std::to_string(day);
   }else{
       int clock_ = ltm->tm_hour;
       int min = ltm->tm_min;
       int sec = ltm->tm_sec;
       return std::to_string(clock_) + "_" + std::to_string(min) + "_" +  std::to_string(sec);
   }
}

struct RobotPos
{
   double x, y, z, a, b, c;
   double dh, dw, v, acc;
   bool weld;    /// 断焊功能开关 false 空走 true 焊接
   static RobotPos instance(string str = "");
   std::string toStr(char sep=',');
   double dis(RobotPos p);
};

double RobotPos::dis(RobotPos p) /// 间距
{
    return sqrt(pow(x-p.x,2)+pow(y-p.y,2)+pow(z-p.z,2));
}

RobotPos RobotPos::instance(string str)
{
    RobotPos p = {0,0,0,0,0,0};
    if(str == "")
        return p;
    char split[3] = {',',' ','\t'};
    std::vector<double> ds;
    for(int i=0;i<3;i++)
    {
        ds.resize(0);
        ds.clear();
        stringstream ss(str);
        while(ss.good())
        {
            string substr;
            getline( ss, substr,split[i]);
            stringstream sd(substr);
            double di;
            sd>>di;
            ds.push_back(di);
        }
        if(ds.size() >= 6)
            break;
    }
    if(ds.size() < 6)
        return p;
    p.x = ds[0];
    p.y = ds[1];
    p.z = ds[2];
    p.a = ds[3];
    p.b = ds[4];
    p.c = ds[5];
    p.v = 0;
    p.acc = 80;
    return p;
}

#endif // PUBLICS_H
